package org.budo.cmbc.direct.sdk.entity;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 电子账户资产信息查询响应对象(不包含产品资产信息)
 *
 * @author caotao
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@ToString
public class MEAccountAssetsQueryResponse extends AbstractCmbcDirectResponse {
    private static final long serialVersionUID = -7782503702210474118L;

    /**
     * 渠道请求流水号
     */
    private String channelJnlNo;

    /**
     * 账户状态(0-正常，1-不正常)
     */
    private String acState;

    /**
     * 活期总余额，Bigdecimal(15,2)。如果为null表示商户配置中不需返回余额
     */
    private String totalBalance;

    /**
     * 活期可用余额,Bigdecimal(15,2)。 如果为null表示商户配置中不需返回余额。
     * 活期可用余额+活期冻结余额=活期总余额，活期可用余额=纯活期可用余额+随心存余额
     */
    private String availableBalance;

    /**
     * 活期冻结余额,Bigdecimal(15,2)
     */
    private String freezeBalance;

    /**
     * 总资产：客户总资产=“活期总余额”+“理财产品-总市值”+“理财产品-总在途”
     */
    private String totalAssets;

    /**
     * 理财产品-总市值
     */
    private String prodTotalAmount;

    /**
     * 理财产品-总在途
     */
    private String prodTotalOtaAmount;

    /**
     * 理财产品收益
     */
    private String prodPayedProfit;

    public MEAccountAssetsQueryResponse(String channelJnlNo, String responseJson) {
        super(channelJnlNo, responseJson);

        JSONObject json = JSON.parseObject(responseJson);
        this.channelJnlNo = json.getString("ChannelJnlNo");
        this.acState = json.getString("AcState");
        this.totalBalance = json.getString("TotalBalance");
        this.availableBalance = json.getString("AvailableBalance");
        this.freezeBalance = json.getString("FreezeBalance");

        this.prodTotalAmount = json.getString("ProdTotalAmount");
        this.prodTotalOtaAmount = json.getString("ProdTotalOtaAmount");

        Double total = 0.0;

        if (!StringUtils.isEmpty(this.totalBalance)) {
            total += Double.valueOf(this.totalBalance);
        }

        if (!StringUtils.isEmpty(this.prodTotalAmount)) {
            total += Double.valueOf(this.prodTotalAmount);
        }

        if (!StringUtils.isEmpty(this.prodTotalOtaAmount)) {
            total += Double.valueOf(this.prodTotalOtaAmount);
        }

        this.totalAssets = total.toString();

        this.prodPayedProfit = "0";

        // 获取如意宝收益
        JSONObject assetsMap = json.getJSONObject("AssetsMap");
        if (assetsMap != null) {
            JSONObject ryb = assetsMap.getJSONObject("RYB");

            if (ryb != null) {
                this.prodPayedProfit = ryb.getString("ProdPayedProfit");
            }
        }
    }
}
